package com.sunwayworld.basemodule.rm.client.service.impl;

import com.sunwayworld.framework.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.client.dao.LimsClientDao;
import com.sunwayworld.basemodule.rm.client.bean.LimsClientBean;
import com.sunwayworld.basemodule.rm.client.service.LimsClientService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;

@Repository
@GikamBean
public class LimsClientServiceImpl implements LimsClientService {

    @Autowired
    private LimsClientDao limsClientDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsClientDao getDao() {
        return limsClientDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsClientBean limsClient = jsonWrapper.parseUnique(LimsClientBean.class);
        limsClient.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsClient);
        return limsClient.getId();
    }

    @Override
    public void enableClient(RestJsonWrapperBean wrapper) {
        List<LimsClientBean> clientBeans = wrapper.parse(LimsClientBean.class);
        clientBeans.forEach(e -> e.setClientStatus(Constant.YES));
        getDao().update(clientBeans,"clientStatus");
    }

    @Override
    public void disableClient(RestJsonWrapperBean wrapper) {
        List<LimsClientBean> clientBeans = wrapper.parse(LimsClientBean.class);
        clientBeans.forEach(e -> e.setClientStatus(Constant.NO));
        getDao().update(clientBeans,"clientStatus");
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.UPDATE)
    public void updatePersons(RestJsonWrapperBean wrapper) {
        List<LimsClientBean> clientBeans = wrapper.parse(LimsClientBean.class);
        getDao().update(clientBeans, "personId","personName");
    }
}
